package cn.wgb.action;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class UploadAction extends ActionSupport {

    //封装上传到服务器的文件对象
    private File upload;

    //封装上传文件的类型
    private String uploadContentType;

    //封装上传文件名称
    private String uploadFileName;

    //获取上传文件路径，是应用上下文中的相对路径
    private String savePath;

    @Override
    public String execute() throws Exception {

        byte[] buffer = new byte[1024];

        //读取文件
        FileInputStream fis = new FileInputStream(getUpload());

        //保存文件
        FileOutputStream fos = new FileOutputStream(getSavePath()+"\\"+this.getUploadFileName());

        int length = fis.read(buffer);

        while (length>0){
            //每次写入length长度的内容
            fos.write(buffer,0,length);
            length = fis.read(buffer);
        }

        fis.close();
        fos.flush();
        fos.close();

        return SUCCESS;
    }

    public File getUpload() {

        return upload;
    }

    public void setUpload(File upload) {

        this.upload = upload;
    }

    public String getUploadContentType() {

        return uploadContentType;
    }

    public void setUploadContentType(String uploadContentType) {

        this.uploadContentType = uploadContentType;
    }

    public String getUploadFileName() {

        return uploadFileName;
    }

    public void setUploadFileName(String uploadFileName) {

        this.uploadFileName = uploadFileName;
    }

    public String getSavePath() {

        //通过读取存放目录获得保存文件的绝对路径
        return ServletActionContext.getServletContext().getRealPath(savePath);
    }

    public void setSavePath(String savePath) {

        this.savePath = savePath;
    }
}
